/*
 * This file is part of connotea-java.
 *
 * connotea-java is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * connotea-java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.connotea;

import static org.apache.commons.lang.builder.ToStringBuilder.reflectionToString;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Post comment entity bean.
 * 
 * @author <a href="mailto:christopher.townson@googlemail.com">Christopher
 *         Townson</a>
 */
public final class Comment implements Comparable<Comment>, Serializable {

    private static final long serialVersionUID = 1L;

    private Date created = new Date();

    private String entry;

    private Date updated;

    /**
     * Default, no-argument constructor creates a Comment instance with defaults
     * (entry is <code>null</code>, created date is "now", updated date is
     * <code>null</code>).
     */
    public Comment() {
        // do nothing
    }

    /**
     * Copy constructor.
     * 
     * @param comment the comment to copy
     */
    public Comment(Comment comment) {
        setCreated(comment.getCreated());
        setEntry(comment.getEntry());
        setUpdated(comment.getUpdated());
    }

    /**
     * Instantiates a Comment with the provided entry.
     * 
     * @param entry the entry
     */
    public Comment(String entry) {
        setEntry(entry);
    }

    /**
     * Comparability is based on entry, created date, then updated date.
     * 
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(Comment comment) {
        return new CompareToBuilder().append(entry, comment.entry).append(
                created, comment.created).append(updated, comment.updated)
                .toComparison();
    }

    /**
     * Equality is based on entry, created date, then updated date.
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Comment)) {
            return false;
        }
        Comment comment = (Comment) obj;
        return new EqualsBuilder().append(entry, comment.entry).append(created,
                comment.created).append(updated, comment.updated).isEquals();
    }

    /**
     * Returns the created date (i.e. date the comment was created on Connotea).
     * 
     * @return the created date
     */
    public Date getCreated() {
        return new Date(created.getTime());
    }

    /**
     * Returns the entry (i.e. comment text).
     * 
     * @return the entry
     */
    public String getEntry() {
        return entry;
    }

    /**
     * Returns the updated date (i.e. date the comment was last updated on
     * Connotea).
     * 
     * @return the updated date
     */
    public Date getUpdated() {
        return (updated != null) ? new Date(updated.getTime()) : null;
    }

    /**
     * HashCode is based on entry, created date, then updated date.
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(entry).append(created).append(
                updated).toHashCode();
    }

    /**
     * Sets the created.
     * 
     * @param created the created to set
     */
    public void setCreated(Date created) {
        this.created = new Date(created.getTime());
    }

    /**
     * Sets the entry.
     * 
     * @param entry the entry to set
     */
    public void setEntry(String entry) {
        this.entry = entry;
    }

    /**
     * Sets the updated.
     * 
     * @param updated the updated to set
     */
    public void setUpdated(Date updated) {
        this.updated = new Date(updated.getTime());
    }

    /**
     * Pretty-prints the current object state.
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return reflectionToString(this);
    }
}
